//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AmericanPayoffAtExpiry.h"
using namespace Cephei::QL::Pricingengines;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (Double spot, Double discount, Double dividendDiscount, Double variance, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CStrikedTypePayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phAmericanPayoffAtExpiry = NULL;
#endif
        QuantLib::Real _spot = (QuantLib::Real)ValueHelper::Convert (spot);
        QuantLib::DiscountFactor _discount = (QuantLib::DiscountFactor)ValueHelper::Convert (discount);
        QuantLib::DiscountFactor _dividendDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (dividendDiscount);
        QuantLib::Real _variance = (QuantLib::Real)ValueHelper::Convert (variance);
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _ppAmericanPayoffAtExpiry = new boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry> (new QuantLib::AmericanPayoffAtExpiry ( _spot,  _discount,  _dividendDiscount,  _variance,  _payoff ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtExpiry = NULL;
#endif
	_ppAmericanPayoffAtExpiry = &childNative;
    
}
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (QuantLib::AmericanPayoffAtExpiry& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAmericanPayoffAtExpiry = NULL;
#endif
	_ppAmericanPayoffAtExpiry = new boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry> (&childNative);
    
    _AmericanPayoffAtExpiryOwner = owner;
    
}

Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (CAmericanPayoffAtExpiry^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtExpiry = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanPayoffAtExpiry = new boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtExpiry = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanPayoffAtExpiry::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (QuantLib::Handle<QuantLib::AmericanPayoffAtExpiry>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPayoffAtExpiry = &childNative;
	_ppAmericanPayoffAtExpiry = &static_cast<boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry>>(childNative.currentLink());
    
    _AmericanPayoffAtExpiryOwner = owner;
}
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (QuantLib::Handle<QuantLib::AmericanPayoffAtExpiry> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPayoffAtExpiry = &childNative;
	_ppAmericanPayoffAtExpiry = &static_cast<boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::CAmericanPayoffAtExpiry (QuantLib::AmericanPayoffAtExpiry childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtExpiry = NULL;
#endif
	_ppAmericanPayoffAtExpiry = new boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry> (new QuantLib::AmericanPayoffAtExpiry (childNative));
    
}
#endif

Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::~CAmericanPayoffAtExpiry ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAmericanPayoffAtExpiry != NULL)
    {
	    delete _ppAmericanPayoffAtExpiry;
        _ppAmericanPayoffAtExpiry = NULL;
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::!CAmericanPayoffAtExpiry ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAmericanPayoffAtExpiry != NULL)
    {
	    delete _ppAmericanPayoffAtExpiry;
    }
}
QuantLib::AmericanPayoffAtExpiry& Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::GetReference ()
{
    if (_ppAmericanPayoffAtExpiry == NULL) throw gcnew NativeNullException ();
	return **_ppAmericanPayoffAtExpiry;
}
boost::shared_ptr<QuantLib::AmericanPayoffAtExpiry>& Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::GetShared ()
{
    if (_ppAmericanPayoffAtExpiry == NULL) throw gcnew NativeNullException ();
	return *_ppAmericanPayoffAtExpiry;
}
QuantLib::AmericanPayoffAtExpiry* Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::GetPointer ()
{
    if (_ppAmericanPayoffAtExpiry == NULL) throw gcnew NativeNullException ();
	return &**_ppAmericanPayoffAtExpiry;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanPayoffAtExpiry>& Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::GetHandle ()
{
	if (_phAmericanPayoffAtExpiry == NULL)
	{
		_phAmericanPayoffAtExpiry = new Handle<QuantLib::AmericanPayoffAtExpiry> (*_ppAmericanPayoffAtExpiry);
	}
	return *_phAmericanPayoffAtExpiry;
}
#endif
bool Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::HasNative () 
{
	return (_ppAmericanPayoffAtExpiry != NULL);
}

Double Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPayoffAtExpiry)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::IAmericanPayoffAtExpiry^ Cephei::QL::Pricingengines::CAmericanPayoffAtExpiry_Factory::Create (Double spot, Double discount, Double dividendDiscount, Double variance, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff)
{
    return gcnew CAmericanPayoffAtExpiry ( spot,  discount,  dividendDiscount,  variance,  payoff);
}
